<html>
	<head>
		<!-- JQuery -->
		<script src="./jsscripts/jquery/jquery.js" type="text/javascript"></script>

		<!-- Colorbox -->		
		<link href="./jsscripts/colorbox/colorbox.css" rel="stylesheet" type="text/css" />
		<script src="./jsscripts/colorbox/jquery.colorbox.js" type="text/javascript"></script>

		<!-- Application -->
		<link href="./css/common.css" rel="stylesheet" type="text/css" />
		
		<script type="text/javascript">
			function DeleteHandler(loan_id)
			{
				if(loan_id == "")
					return false;
					
				var isConfirmed = confirm("Do you really want to delete the seleted loan?");
				if(isConfirmed)
				{
					return true;
				}
				
				return false;
			}
		</script>
	</head>
	<body>
<?php
	$mysql_connect = mysql_connect('localhost', 'hr_mysql', 'hr_mysql');
	if ($mysql_connect)
	{		
		mysql_select_db('hr_mysql', $mysql_connect);
		
		$loan_id = isset($_GET["loan_id"]) ? $_GET["loan_id"] : 0;
		if($loan_id != 0)
		{
			$mysql_query = "delete from hs_hr_loan where id = '$loan_id'";			
			mysql_query($mysql_query);
		}
		
		$mysql_query = "select a.*, b.emp_firstname from hs_hr_loan a inner join hs_hr_employee b on a.employee_id = b.employee_id order by b.emp_firstname";
		$mysql_result = mysql_query($mysql_query);
		if(!$mysql_result)		
			exit;
			
		$loan_list = array();
		while($mysql_row = mysql_fetch_array($mysql_result))
			$loan_list[] = $mysql_row;			
	}
?>
		<div id="div_content">
			<div style="text-align:center; border:1px solid;font-weight:bold;padding:5px;">
				Loan
			</div>
			<div style="float:right; margin-top:5px; margin-bottom:5px;">
                <input type="image" class="loan_insert" id="img_loan" src="./images/add.png" value="Insert loan" alt="Insert loan" title=""/>
            </div>
			<div stlyle="clear:both;">&nbsp;</div>
			<div>
				<table id="table_header">
					<thead>
						<tr>
							<td>No</td>
							<td>Name</td>
							<td>Date</td>
							<td>Amount</td>
							<td style="width:110px;">&nbsp;</td>
						</tr>
					</thead>
					<tbody>	
<?php
	$counter = 0;
	foreach($loan_list as &$mysql_row)
	{	
		$counter += 1;
?>
						<tr>
							<td><?php echo $counter; ?></td>
							<td><?php echo $mysql_row['emp_firstname'];?></td>
							<td class="date"><?php echo $mysql_row['date'];?></td>
							<td class="number"><?php echo number_format($mysql_row['amount'], 2); ?></td>
							<td style="text-align:center;">
								<a href="./loan_handler.php?action=edit&loan_id=<?php echo $mysql_row['id'];?>" class="loan_edit">
									<img src="./images/edit.png" />
								<a href="./loan.php?loan_id=<?php echo $mysql_row['id'];?>" onclick="return DeleteHandler('<?php echo $mysql_row['id'];?>');">
									<img src="./images/delete.png" />
								</a>
							</td>
						</tr>
<?php
	}
?>					
					</tbody>
				</table>
		</div>		
		
		<script type="text/javascript">
			var isUpdated = false;
			$(function() 
			{
				$(".loan_insert").colorbox(
				{
					width: '460px',
					height: '340px',
					iframe: true,
					href: './loan_handler.php',
					onClosed:function(){ if(isUpdated) location.reload(true); }
				});

				$(".loan_edit").colorbox(
				{
					width: '800px',
					height: '450px',
					iframe: true
				});
			});
		</script>		
	</body>
</html>
